/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Material.java
 *
 * Created on 09/03/2011, 20:47:36
 */

package sisgraf.view;
import java.util.List;
import java.math.BigDecimal;
import sisgraf.model.Material;
import sisgraf.controler.MaterialControler;
import sisgraf.controler.UnidadeControler;
import sisgraf.controler.CorControler;
import sisgraf.model.Unidade;
import sisgraf.model.Cor;
import sisgraf.dao.UnidadeDaoImp;
import sisgraf.dao.CorDaoImp;
/**
 *
 * @author jordan
 */
public class Materiais extends javax.swing.JDialog {
    private int opcao=0;
    private MaterialControler materialControler;
    private Material material;
    private List<Unidade> listaU = new UnidadeDaoImp().list("");
    private List<Cor> listaC = new CorDaoImp().list("");
    private Integer chaveUnidade[];
    private Integer chaveCor[];
    private UnidadeControler unidadeControler =new UnidadeControler();
    private CorControler corControler =new CorControler();
    private boolean incluiu=false;
    /** Creates new form Material */
    public Materiais(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    public void setOpcao(int opcao)
    {
        this.opcao = opcao;
        formComponentResized(null);
        setLocationRelativeTo(null);
        Integer chaveUnd[] = new Integer[listaU.size()];
        chaveUnidade=chaveUnd;
        for (int i = 0; i<listaU.size();i++){
             jCbBxUnidade.addItem(listaU.get(i).getDescricao());
             chaveUnidade[i]=listaU.get(i).getId();
             if (listaU.get(i).getDescricao().equals(material.getUnidade().getDescricao()))
                jCbBxUnidade.setSelectedIndex(i);
        }
        Integer chaveCr[] = new Integer[listaC.size()];
        chaveCor=chaveCr;
        for (int i = 0; i<listaC.size();i++){
             jCbBxCor.addItem(listaC.get(i).getDescricao());
             chaveCor[i]=listaC.get(i).getId();
             if (listaC.get(i).getDescricao().equals(material.getCor().getDescricao()))
                jCbBxCor.setSelectedIndex(i);
        }
        jTxFdDescricao.setText(material.getDescricao());
        if (this.opcao == Menu.OP_PESQ_ALT){
            jTxFdCodigo.setText(material.getId().toString());
            jTxFdCodEst.setText(material.getCodigo().toString());
            jTxFdCusto.setText(material.getCusto()==null?"":material.getCusto().toString());
            jTxFdLargura.setText(material.getLargura()==null?"":material.getLargura().toString());
            jTxFdComprimento.setText(material.getComprimento()==null?"":material.getComprimento().toString());
            jTxFdGramatura.setText(material.getGramatura()==null?"":material.getGramatura().toString());
            jTxFdRendimento.setText(material.getRendimento()==null?"":material.getRendimento().toString());
            if (material.getTipo().equals("plano")){
                jRdBtPlano.setSelected(true);
                jTxFdRendimento.setEnabled(false);
            } else if (material.getTipo().equals("aplicacao")){
                jRdBtAplicacao.setSelected(true);
                jTxFdLargura.setEnabled(false);
                jTxFdComprimento.setEnabled(false);
                jTxFdGramatura.setEnabled(false);
            } else {
                jRdBtServico.setSelected(true);
                jTxFdLargura.setEnabled(false);
                jTxFdComprimento.setEnabled(false);
                jTxFdGramatura.setEnabled(false);
                jTxFdRendimento.setEnabled(false);
                jCbBxCor.setEnabled(false);
            }
            if (material.getAtivo().equals("S"))
                jCkBxAtivo.setSelected(true);
            else
                jCkBxAtivo.setSelected(false);
            jTxFdCodEst.setEditable(false);
        } else {
            jTxFdCodigo.setText("");
            jTxFdCusto.setText("");
            jTxFdLargura.setText("");
            jTxFdComprimento.setText("");
            jTxFdGramatura.setText("");
            jTxFdRendimento.setText("");
            jCbBxUnidade.setSelectedIndex(-1);
            jCbBxCor.setSelectedIndex(-1);
            jRdBtPlano.setSelected(false);
            jRdBtAplicacao.setSelected(false);
            jRdBtServico.setSelected(false);
            jCkBxAtivo.setSelected(false);
            jTxFdLargura.setEnabled(false);
            jTxFdComprimento.setEnabled(false);
            jTxFdGramatura.setEnabled(false);
            jTxFdRendimento.setEnabled(false);
        }
        jTxFdCodigo.setEnabled(false);
        setVisible(true);
        jTxFdDescricao.setFocusable(true);
    }
    public void setMaterial(Material material){
        this.material = material;
    }
    public void setControler(MaterialControler materialControler){
        this.materialControler = materialControler;
    }
    public boolean getncluiu(){
        return this.incluiu;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btGpTipo = new javax.swing.ButtonGroup();
        jPnlSuperior = new javax.swing.JPanel();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        jLbSubTitulo = new javax.swing.JLabel();
        jPnlCentral = new javax.swing.JPanel();
        jPnlConteudo = new javax.swing.JPanel();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        jTxFdCodigo = new javax.swing.JTextField();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        jTxFdDescricao = new javax.swing.JTextField();
        javax.swing.JLabel jLabel7 = new javax.swing.JLabel();
        jCbBxUnidade = new javax.swing.JComboBox();
        javax.swing.JLabel jLabel8 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel9 = new javax.swing.JLabel();
        jCbBxCor = new javax.swing.JComboBox();
        jCkBxAtivo = new javax.swing.JCheckBox();
        javax.swing.JPanel jPanel1 = new javax.swing.JPanel();
        jRdBtPlano = new javax.swing.JRadioButton();
        jRdBtAplicacao = new javax.swing.JRadioButton();
        jRdBtServico = new javax.swing.JRadioButton();
        javax.swing.JPanel jPanel2 = new javax.swing.JPanel();
        javax.swing.JLabel jLabel10 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel11 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel12 = new javax.swing.JLabel();
        jTxFdLargura = new javax.swing.JFormattedTextField();
        jTxFdComprimento = new javax.swing.JFormattedTextField();
        jTxFdGramatura = new javax.swing.JFormattedTextField();
        javax.swing.JPanel jPanel3 = new javax.swing.JPanel();
        javax.swing.JPanel jPanel4 = new javax.swing.JPanel();
        javax.swing.JLabel jLabel13 = new javax.swing.JLabel();
        jTxFdRendimento = new javax.swing.JFormattedTextField();
        jTxFdCusto = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jTxFdCodEst = new javax.swing.JTextField();
        jPnlControle = new javax.swing.JPanel();
        jBtFinalizar = new javax.swing.JButton();
        jBtConfirmar = new javax.swing.JButton();
        jPnlInferior = new javax.swing.JPanel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jPnlSuperior.setBorder(new javax.swing.border.MatteBorder(null));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("SISTEMA DE ORÇAMENTOS PARA SERVIÇOS GRAFICOS");
        jLabel1.setMinimumSize(new java.awt.Dimension(0, 14));

        jLbSubTitulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLbSubTitulo.setText("Materiais");
        jLbSubTitulo.setMinimumSize(new java.awt.Dimension(0, 14));

        javax.swing.GroupLayout jPnlSuperiorLayout = new javax.swing.GroupLayout(jPnlSuperior);
        jPnlSuperior.setLayout(jPnlSuperiorLayout);
        jPnlSuperiorLayout.setHorizontalGroup(
            jPnlSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 562, Short.MAX_VALUE)
            .addComponent(jLbSubTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, 562, Short.MAX_VALUE)
        );
        jPnlSuperiorLayout.setVerticalGroup(
            jPnlSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlSuperiorLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLbSubTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPnlCentral.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPnlCentral.setMinimumSize(new java.awt.Dimension(100, 0));

        jPnlConteudo.setBorder(javax.swing.BorderFactory.createLineBorder(null));

        jLabel5.setText("Código:");

        jLabel6.setText("Descrição:");

        jLabel7.setText("Unidade:");

        jLabel8.setText("Cor:");

        jLabel9.setText("Custo:");

        jCkBxAtivo.setText("Ativo");
        jCkBxAtivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCkBxAtivoActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btGpTipo.add(jRdBtPlano);
        jRdBtPlano.setText("Plano");
        jRdBtPlano.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRdBtPlanoActionPerformed(evt);
            }
        });

        btGpTipo.add(jRdBtAplicacao);
        jRdBtAplicacao.setText("Aplicação");
        jRdBtAplicacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRdBtAplicacaoActionPerformed(evt);
            }
        });

        btGpTipo.add(jRdBtServico);
        jRdBtServico.setText("Outros");
        jRdBtServico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRdBtServicoActionPerformed(evt);
            }
        });

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel10.setText("Largura:");

        jLabel11.setText("Comprimento:");

        jLabel12.setText("Gramatura:");

        jTxFdLargura.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        jTxFdLargura.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jTxFdComprimento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("0"))));
        jTxFdComprimento.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jTxFdGramatura.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        jTxFdGramatura.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTxFdGramatura, 0, 0, Short.MAX_VALUE)
                    .addComponent(jTxFdLargura, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTxFdComprimento, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                .addGap(34, 34, 34))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jTxFdLargura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTxFdComprimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jTxFdGramatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 133, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 118, Short.MAX_VALUE)
        );

        jPanel4.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel13.setText("Rendimento:");

        jTxFdRendimento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("##0"))));
        jTxFdRendimento.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTxFdRendimento, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jTxFdRendimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(81, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jRdBtPlano, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jRdBtAplicacao, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jRdBtServico, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRdBtPlano)
                    .addComponent(jRdBtAplicacao)
                    .addComponent(jRdBtServico))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jTxFdCusto.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.00"))));
        jTxFdCusto.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        jLabel2.setText("Estoque:");

        javax.swing.GroupLayout jPnlConteudoLayout = new javax.swing.GroupLayout(jPnlConteudo);
        jPnlConteudo.setLayout(jPnlConteudoLayout);
        jPnlConteudoLayout.setHorizontalGroup(
            jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlConteudoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnlConteudoLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCbBxUnidade, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8)
                        .addGap(4, 4, 4)
                        .addComponent(jCbBxCor, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxFdCusto, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCkBxAtivo, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPnlConteudoLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxFdCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxFdCodEst, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTxFdDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPnlConteudoLayout.setVerticalGroup(
            jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlConteudoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTxFdDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jTxFdCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTxFdCodEst, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jCbBxUnidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jCbBxCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jCkBxAtivo)
                    .addComponent(jTxFdCusto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPnlControle.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jBtFinalizar.setText("Finalizar");
        jBtFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtFinalizarActionPerformed(evt);
            }
        });

        jBtConfirmar.setText("Confirmar");
        jBtConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtConfirmarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPnlControleLayout = new javax.swing.GroupLayout(jPnlControle);
        jPnlControle.setLayout(jPnlControleLayout);
        jPnlControleLayout.setHorizontalGroup(
            jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlControleLayout.createSequentialGroup()
                .addContainerGap(392, Short.MAX_VALUE)
                .addComponent(jBtConfirmar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBtFinalizar)
                .addContainerGap())
        );
        jPnlControleLayout.setVerticalGroup(
            jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlControleLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtFinalizar)
                    .addComponent(jBtConfirmar))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPnlCentralLayout = new javax.swing.GroupLayout(jPnlCentral);
        jPnlCentral.setLayout(jPnlCentralLayout);
        jPnlCentralLayout.setHorizontalGroup(
            jPnlCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPnlConteudo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlControle, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPnlCentralLayout.setVerticalGroup(
            jPnlCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlCentralLayout.createSequentialGroup()
                .addComponent(jPnlConteudo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlControle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPnlInferior.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Gráfica Modêlo");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Grupo de Estudo PMA Anhanguera");

        javax.swing.GroupLayout jPnlInferiorLayout = new javax.swing.GroupLayout(jPnlInferior);
        jPnlInferior.setLayout(jPnlInferiorLayout);
        jPnlInferiorLayout.setHorizontalGroup(
            jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlInferiorLayout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 244, Short.MAX_VALUE)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPnlInferiorLayout.setVerticalGroup(
            jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPnlSuperior, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlInferior, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlCentral, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPnlSuperior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlCentral, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlInferior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        // TODO add your handling code here:
        jPnlControle.setBorder(null);
        jPnlConteudo.setBorder(null);
        int dif = this.getHeight()-jPnlSuperior.getHeight()-jPnlCentral.getHeight()-jPnlInferior.getHeight()-6;
        jPnlCentral.setSize(this.getWidth(),this.getHeight()-jPnlSuperior.getHeight()-jPnlInferior.getHeight()-dif);
        jPnlConteudo.setSize(jPnlCentral.getWidth()-12,jPnlCentral.getHeight()-jPnlControle.getHeight());
        jPnlControle.setSize(jPnlCentral.getWidth()-12, jPnlControle.getHeight());
        jPnlControle.setLocation(2, jPnlConteudo.getHeight());
        if (this.opcao == Menu.OP_PESQ_INC) jLbSubTitulo.setText("Inclusão Materiais");
        if (this.opcao == Menu.OP_PESQ_ALT) jLbSubTitulo.setText("Alteração Materiais");
    }//GEN-LAST:event_formComponentResized

    private void jCkBxAtivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCkBxAtivoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCkBxAtivoActionPerformed

    private void jBtFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtFinalizarActionPerformed
        if (
                (!jTxFdCodigo.getText().equals((material.getId()==null)?"":material.getId().toString()))||
                (!jTxFdDescricao.getText().equals(material.getDescricao()==null?"":material.getDescricao()))||
                (!(jCbBxUnidade.getSelectedItem()==null?
                    jCbBxUnidade.getSelectedItem()==material.getUnidade().getDescricao():
                    jCbBxUnidade.getSelectedItem().equals(material.getUnidade()==null?
                        "":material.getUnidade().getDescricao())))||
                (!(jCbBxCor.getSelectedItem()==null?
                    jCbBxCor.getSelectedItem()==material.getCor().getDescricao():
                    jCbBxCor.getSelectedItem().equals(material.getCor()==null?
                        "":material.getCor().getDescricao())))||
                (!jTxFdCusto.getText().equals(material.getCusto()==null?"":material.getCusto().toString()))||
                (!jTxFdLargura.getText().equals(material.getLargura()==null?"":material.getLargura().toString()))||
                (!jTxFdComprimento.getText().equals(material.getComprimento()==null?"":material.getComprimento().toString()))||
                (!jTxFdGramatura.getText().equals(material.getGramatura()==null?"":material.getGramatura().toString()))||
            (!jTxFdRendimento.getText().equals(material.getRendimento()==null?"":material.getRendimento().toString()))
           )
        {
            if (javax.swing.JOptionPane.showConfirmDialog(null,
                    "Modificações foram feitas, deseja realmente finalizar sem salvar ?",
                    "Finalizar", javax.swing.JOptionPane.YES_NO_OPTION)==0){
               Materiais.this.dispose();
            }
        } else Materiais.this.dispose();

    }//GEN-LAST:event_jBtFinalizarActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        materialControler.liberarMaterial();

    }//GEN-LAST:event_formWindowClosed

    private void jBtConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtConfirmarActionPerformed
        // TODO add your handling code here:
        boolean gravarOk = true;
        String mensErro="Dados não puderam ser salvos, apresentaram os seguintes erro:";
        //##
        String descricao=jTxFdDescricao.getText();
        int unidade=jCbBxUnidade.getSelectedIndex();
        int cor = jCbBxCor.getSelectedIndex();
        String custo = jTxFdCusto.getText();
        String largura = jTxFdLargura.getText();
        String comprimento = jTxFdComprimento.getText();
        String gramatura = jTxFdGramatura.getText();
        String rendimento = jTxFdRendimento.getText();
        String tipo;
        if (btGpTipo.getSelection()==jRdBtPlano.getModel()){
            tipo=("plano");
        } else if (btGpTipo.getSelection()==jRdBtAplicacao.getModel()){
            tipo=("aplicacao");
        } else if (btGpTipo.getSelection()==jRdBtServico.getModel()){
            tipo=("outros");
        } else {
            tipo=("");
        }
        if (descricao==null||descricao.equals("")){
            gravarOk=false;
            mensErro+="\nCampo de descrição deve ser preenchido!!";
        }
        if (unidade<0){
            gravarOk=false;
            mensErro+="\nDeve ser selecionada uma unidade para o material!!";
        }
        if (cor<0&&!tipo.equals("servico")){
            gravarOk=false;
            mensErro+="\nDeve ser selecionada uma cor para o material!!";
        }
        if (cor<0){
            gravarOk=false;
            mensErro+="\nDeve ser selecionada uma cor para o material!!";
        }
        if (custo==null||custo.equals("")){
            gravarOk=false;
            mensErro+="\nCampo custo deve ser preenchido!!";
        }
        if ((largura==null||largura.equals(""))&&tipo.equals("plano")){
            gravarOk=false;
            mensErro+="\nCampo largura deve ser preenchido!!";
        }
        if ((comprimento==null||comprimento.equals(""))&&(tipo.equals("plano"))){
            gravarOk=false;
            mensErro+="\nCampo comprimento deve ser preenchido!!";
        }
        if ((gramatura==null||gramatura.equals(""))&&(tipo.equals("plano"))){
            gravarOk=false;
            mensErro+="\nCampo gramatura deve ser preenchido!!";
        }
        if ((rendimento==null||rendimento.equals(""))&&(tipo.equals("aplicação"))){
            gravarOk=false;
            mensErro+="\nCampo rendimento deve ser preenchido!!";
        }
        if (tipo.equals("")||tipo==null){
            gravarOk=false;
            mensErro+="\nCampo tipo deve ser selecionado!!";
        }
        if (!gravarOk){
           javax.swing.JOptionPane.showMessageDialog(null,
                   mensErro+"\n"
                   + "Corrija as infromações listadas e tente confirmar novamente ",
                   "Acesso ao Banco de Dados", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        } else {
            material.setDescricao(descricao);
            material.setUnidade(unidadeControler.leUnidade(chaveUnidade[unidade]));
//            if (cor==-1)
//                material.setCor(null);
//            else
                material.setCor(corControler.leCor(chaveCor[cor]));
            if (custo.equals(""))
                material.setCusto(null);
            else{
                custo=custo.replace(',', '.');
                double custoDb = Double.parseDouble(custo);
                BigDecimal custoBd= BigDecimal.valueOf(custoDb);
                material.setCusto(custoBd);
            }
            if (largura.equals(""))
                material.setLargura(null);
            else
                material.setLargura(Integer.parseInt(largura));
            if (comprimento.equals(""))
                material.setComprimento(null);
            else
            material.setComprimento(Integer.parseInt(comprimento));
            if (gramatura.equals(""))
                material.setGramatura(null);
            else
                material.setGramatura(Integer.parseInt(gramatura));
            if (rendimento.equals(""))
                material.setRendimento(null);
            else
                material.setRendimento(Integer.parseInt(rendimento));
            if (tipo.equals(""))
                material.setTipo(null);
            else
                material.setTipo(tipo);
            if (tipo.equals("plano")){
                material.setRendimento(null);
            }
            if (tipo.equals("aplicação")){
                material.setComprimento(null);
                material.setLargura(null);
                material.setGramatura(null);
            }
            if (tipo.equals("servico")){
                material.setComprimento(null);
                material.setLargura(null);
                material.setGramatura(null);
//                material.setCor(null);
            }
            if (this.opcao == Menu.OP_PESQ_ALT){
               if (!materialControler.alterarMaterial(material)){
                  javax.swing.JOptionPane.showMessageDialog(null,
                    "Erro na atualização dos dados do material",
                    "Acesso ao Banco de Dados",
                    javax.swing.JOptionPane.INFORMATION_MESSAGE);
                }
            } else if (this.opcao == Menu.OP_PESQ_INC){
                boolean teste=materialControler.adicionarMaterial(material);
                if (!teste){
                    javax.swing.JOptionPane.showMessageDialog(null,
                        "Erro na inclusão do material",
                        "Acesso ao Banco de Dados",
                        javax.swing.JOptionPane.INFORMATION_MESSAGE);
                } this.incluiu = true;
            }
            Materiais.this.dispose();
        }
    }//GEN-LAST:event_jBtConfirmarActionPerformed

    private void jRdBtPlanoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRdBtPlanoActionPerformed
        // TODO add your handling code here:
        if (jRdBtPlano.isEnabled()){
           jTxFdLargura.setEnabled(true);
           jTxFdComprimento.setEnabled(true);
           jTxFdGramatura.setEnabled(true);
           jTxFdRendimento.setEnabled(false);
           jTxFdRendimento.setText("");
           jCbBxCor.setEnabled(true);
        }
    }//GEN-LAST:event_jRdBtPlanoActionPerformed

    private void jRdBtAplicacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRdBtAplicacaoActionPerformed
        // TODO add your handling code here:
        if (jRdBtAplicacao.isEnabled()){
           jTxFdLargura.setEnabled(false);
           jTxFdComprimento.setEnabled(false);
           jTxFdGramatura.setEnabled(false);
           jTxFdLargura.setText("");
           jTxFdComprimento.setText("");
           jTxFdGramatura.setText("");
           jTxFdRendimento.setEnabled(true);
           jCbBxCor.setEnabled(true);
        }
    }//GEN-LAST:event_jRdBtAplicacaoActionPerformed

    private void jRdBtServicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRdBtServicoActionPerformed
        // TODO add your handling code here:
        if (jRdBtAplicacao.isEnabled()){
           jTxFdLargura.setEnabled(false);
           jTxFdComprimento.setEnabled(false);
           jTxFdGramatura.setEnabled(false);
           jTxFdRendimento.setEnabled(false);
           jTxFdLargura.setText("");
           jTxFdComprimento.setText("");
           jTxFdGramatura.setText("");
           jTxFdRendimento.setText("");
           jCbBxCor.setEnabled(true);
        }
    }//GEN-LAST:event_jRdBtServicoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Materiais dialog = new Materiais(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btGpTipo;
    private javax.swing.JButton jBtConfirmar;
    private javax.swing.JButton jBtFinalizar;
    private javax.swing.JComboBox jCbBxCor;
    private javax.swing.JComboBox jCbBxUnidade;
    private javax.swing.JCheckBox jCkBxAtivo;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLbSubTitulo;
    private javax.swing.JPanel jPnlCentral;
    private javax.swing.JPanel jPnlConteudo;
    private javax.swing.JPanel jPnlControle;
    private javax.swing.JPanel jPnlInferior;
    private javax.swing.JPanel jPnlSuperior;
    private javax.swing.JRadioButton jRdBtAplicacao;
    private javax.swing.JRadioButton jRdBtPlano;
    private javax.swing.JRadioButton jRdBtServico;
    private javax.swing.JTextField jTxFdCodEst;
    private javax.swing.JTextField jTxFdCodigo;
    private javax.swing.JFormattedTextField jTxFdComprimento;
    private javax.swing.JFormattedTextField jTxFdCusto;
    private javax.swing.JTextField jTxFdDescricao;
    private javax.swing.JFormattedTextField jTxFdGramatura;
    private javax.swing.JFormattedTextField jTxFdLargura;
    private javax.swing.JFormattedTextField jTxFdRendimento;
    // End of variables declaration//GEN-END:variables

}
